package com.sunwhite.deeplearning.demo02;

import org.neuroph.core.Layer;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.comp.neuron.ThresholdNeuron;
import org.neuroph.nnet.learning.BinaryDeltaRule;
import org.neuroph.util.*;

/***
 *创建一个简单的感知机
 */
public class NetWork extends NeuralNetwork {
    public void createNetWork(int inputNeuronsCount, int outputNeuronsCount, TransferFunctionType transferFunctionType){
        //设置神经网络类型 我们这里将类型设置为感知器
        this.setNetworkType(NeuralNetworkType.PERCEPTRON);
        //初始化神经元输入刺激设置
        NeuronProperties inputNeuronProperties = new NeuronProperties();
        inputNeuronProperties.setProperty("transferFunction", TransferFunctionType.LINEAR);

        //创建输入刺激
        Layer inputLayer = LayerFactory.createLayer(inputNeuronsCount, inputNeuronProperties);
        this.addLayer(inputLayer);

        //初始化神经元输出刺激设置
        NeuronProperties outPutNeuronProperties = new NeuronProperties();
        outPutNeuronProperties.setProperty("neuronType", ThresholdNeuron.class);
        outPutNeuronProperties.setProperty("thresh",new Double(Math.abs(Math.random())));
        outPutNeuronProperties.setProperty("transferFunction",transferFunctionType);

        //为sigmoid和tanh传输函数设置斜率属性
        outPutNeuronProperties.setProperty("transferFunction.slope",new Double(1));

        //创建一个神经元输出
        Layer outputLayer = LayerFactory.createLayer(outputNeuronsCount, outPutNeuronProperties);
        this.addLayer(outputLayer);

        //在输出和输出层中建立安全连接
        ConnectionFactory.fullConnect(inputLayer,outputLayer);

        //为神经网络设置默认输入输出
        NeuralNetworkFactory.setDefaultIO(this);
        this.setLearningRule(new BinaryDeltaRule());
    }
}
